<template>
  <basic-dialog
    title="高级关联"
    width="40%"
    okText="高级查询"
    :visible="visible"
    :show-close="false"
    :handleClose="handleCancel"
    :handleOk="handleSeniorSearch"
  >
    <BasicTable
      :tableHeight="tableHeight"
      :showSelectCol="false"
      :showPagination="false"
      :data-source="tableInfo.list"
      :columns="tableInfo.columns"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicTable from '@/components/BasicTable';
export default {
  components: {
    BasicDialog,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // 表格高度
    tableHeight: {
      type: Number,
      default: 480,
    },
    // 最大提交量，默认1000
    submitLimitLength: {
      type: Number,
      default: 1000
    },
  },
  data () {
    return {
      tableInfo: {
        list: [{ skuNo: '', replaceSkuNo: '' }],
        columns: [
          {
            title: 'SKU编码',
            render: (h, { row }) => {
              return this.getTextareaColRender(h, row, 'skuNo', 'SKU编码');
            }
          },
          {
            title: '替代商品编码',
            render: (h, { row }) => {
              return this.getTextareaColRender(h, row, 'replaceSkuNo', '替代商品编码');
            }
          }
        ]
      }
    }
  },
  computed: {
    // textarea高度
    textareaRows () {
      return Math.floor(this.tableHeight / 24);
    },
  },
  methods: {
    getTextareaColRender (h, row, key, label) {
      return <el-input
        v-model={row[key]}
        type="textarea"
        resize="none"
        autosize={{ minRows: this.textareaRows, maxRows: this.textareaRows }}
        placeholder={`请输入${label}`}
      ></el-input>
    },
    // 获取切割文本后的集合
    getSplitedTargets (dataSource) {
      const result = {};
      for (let key in dataSource) {
        // 去除空格字符，根据回车换行符切割
        result[key] = dataSource[key].replace(/[ ]/g, '').split(/\n/);
      }
      return result;
    },
    // 校验SKU编码是否符合规则
    validateSkuNo (list) {
      const isEmpty = list.every(item => !item.skuNo);
      if (isEmpty) {
        return this.$message.warning('SKU编码不能为空');
      }
      const isUnqualified = list.some(item => item.replaceSkuNo && !item.skuNo);
      if (isUnqualified) {
        return this.$message.warning('替代编码对应的SKU编码不能为空');
      }
      return false;
    },
    // 获取执行所有任务后的结果
    getResultsByRunAllTasks (dataSource) {
      const contents = this.getSplitedTargets(dataSource);
      const restructuringList = contents.skuNo.map((item, index) => ({
        skuNo: item, replaceSkuNo: contents.replaceSkuNo[index]
      }));
      const isUnMatched = this.validateSkuNo(restructuringList);
      if (isUnMatched) return false;
      const results = restructuringList.filter(item => item.skuNo);
      return results;
    },
    // 高级查询
    handleSeniorSearch () {
      const result = this.getResultsByRunAllTasks(this.tableInfo.list[0]);
      if (result.length > this.submitLimitLength) {
        return this.$message.warning(`当前提交的行数不能超过${this.submitLimitLength}`);
      }
      if (result) {
        this.$emit('seniorSearchResult', result);
        this.$emit('update:visible', false);
      }
    },
    // 取消
    handleCancel () {
      this.$emit('update:visible', false);
    },
    // 重置数据
    resetAllData () {
      this.tableInfo.list = [{ skuNo: '', replaceSkuNo: '' }];
    }
  }
}
</script>
